﻿using HpSocketFunction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace BLL
{
    class UperComputerRecevie
    {
        bool _BreakConnState;
        public Dictionary<string, string> UperComputerRecevieIO = new Dictionary<string, string>()
        {
            ["Order"] = "",
            ["StartWorkState"] = "",
            ["ReportWorkState"] = "",
            ["AGV1State"] = "",
            ["AGV2State"] = ""
        };
        public UperComputerRecevie(HpSocketClient UperComputerClient)
        {
            UperComputerClient.OnReceiveMessage += OnReceiveMessage;
            UperComputerClient.SocketConnOline += ConnectState;
        }
        public void OnReceiveMessage(string TargetIp, int targetPort, byte[] data)
        {
            string ReceiveMessage = Encoding.GetEncoding("gb18030").GetString(data);
            DAL.LogSingle.Instaince.WriteMainLog("服务器:" + ReceiveMessage);
            string[] DataList = ReceiveMessage.Split(',');
            switch (DataList[0])
            {
                case "Order":
                    UperComputerRecevieIO["Order"] = DataList[1];
                    break;
                case "StartWorkState":
                    UperComputerRecevieIO["StartWorkState"] = DataList[1];
                    break;
                case "ReportWorkState":
                    UperComputerRecevieIO["ReportWorkState"] = DataList[1];
                    break;
                case "AGV1State":
                    UperComputerRecevieIO["AGV1State"] = DataList[1];
                    break;
                case "AGV2State":
                    UperComputerRecevieIO["AGV2State"] = DataList[1];
                    break;
                default:
                    break;
            }
        }
        public void ConnectState(string TargetIp, int targetPort, bool AddOrRemove)
        {
            if (AddOrRemove)
            {
                DAL.LogSingle.Instaince.WriteMainLog("服务器已经连接");
                _BreakConnState = false;
            }
            else if (!_BreakConnState)
            {
                DAL.LogSingle.Instaince.WriteMainLog("服务器断开");
                _BreakConnState = true;
            }
        }
    }
    class CCDRecevie
    {
        bool _BreakConnState;
        public Dictionary<string, string> CCDRecevieIO = new Dictionary<string, string>()
        {
            ["Fetch"] = "",
            ["Fetch1"] = "",
            ["Fetch2"] = "",
            ["Fetch3"] = "",
            ["Put"] = "",
            ["Diameter"] = "",
            ["Path"] = ""
        };
        public CCDRecevie(HpSocketClient CCDClient)
        {
            CCDClient.OnReceiveMessage += OnReceiveMessage;
            CCDClient.SocketConnOline += ConnectState;
        }
        public void OnReceiveMessage(string TargetIp, int targetPort, byte[] data)
        {
            string ReceiveMessage = Encoding.GetEncoding("gb18030").GetString(data);
            DAL.LogSingle.Instaince.WriteMainLog("CCD:" + ReceiveMessage);
            string[] DataList = ReceiveMessage.Split(',');
            switch (DataList[0])
            {
                case "nj":
                    CCDRecevieIO["Diameter"] = DataList[1];
                    break;
                case "q":
                    CCDRecevieIO["Fetch"] = DataList[1] + "-" + DataList[2];
                    break;
                case "q1":
                    CCDRecevieIO["Fetch1"] = DataList[1] + "-" + DataList[2];
                    break;
                case "q2":
                    CCDRecevieIO["Fetch2"] = DataList[1] + "-" + DataList[2];
                    break;
                case "q3":
                    CCDRecevieIO["Fetch3"] = DataList[1] + "-" + DataList[2];
                    break;
                case "f":
                    CCDRecevieIO["Put"] = DataList[1] + "-" + DataList[3] + "," + DataList[2] + "-" + DataList[4];
                    break;
                default:
                    break;
            }

        }
        public void ConnectState(string TargetIp, int targetPort, bool AddOrRemove)
        {
            if (AddOrRemove)
            {
                DAL.LogSingle.Instaince.WriteMainLog("CCD已经连接");
                _BreakConnState = false;
            }
            else if (!_BreakConnState)
            {
                DAL.LogSingle.Instaince.WriteMainLog("CCD断开");
                _BreakConnState = true;
            }
        }
    }
    public class RobotRecevie
    {
        bool _BreakConnState;
        public Dictionary<string, string> RobotRecevieIO = new Dictionary<string, string>()
        {
            ["Fetch"] = "",
            ["Put"] = "",
            ["TestWeight"] = "",
            ["TestHeight"] = "",
            ["ScanOK"] = "",
            ["ScanNG"] = "",
        };
        public RobotRecevie(HpSocketClient RobotClient)
        {
            RobotClient.OnReceiveMessage += OnReceiveMessage;
            RobotClient.SocketConnOline += ConnectState;
        }
        public void OnReceiveMessage(string TargetIp, int targetPort, byte[] data)
        {
            string ReceiveMessage = Encoding.GetEncoding("gb18030").GetString(data);
            DAL.LogSingle.Instaince.WriteMainLog("机器人:" + ReceiveMessage);
            string[] DataList = ReceiveMessage.Split(',');
            switch (DataList[0])
            {
                case "QL":
                    RobotRecevieIO["Fetch"] = DataList[1];
                    break;
                case "FL":
                    RobotRecevieIO["Put"] = DataList[1] + "-" + DataList[2];
                    break;
                case "TestW":
                    RobotRecevieIO["TestWeight"] = DataList[1] + "-" + DataList[2];
                    break;
                case "TestH":
                    RobotRecevieIO["TestHeight"] = DataList[1] + "-" + DataList[2];
                    break;
                case "Scan":
                    RobotRecevieIO["Scan"] = DataList[1] + "-" + DataList[2];
                    break;
                default:
                    break;
            }
        }
        public void ConnectState(string TargetIp, int targetPort, bool AddOrRemove)
        {
            if (AddOrRemove)
            {
                DAL.LogSingle.Instaince.WriteMainLog("机器人已经连接");
                _BreakConnState = false;
            }
            else if (!_BreakConnState)
            {
                DAL.LogSingle.Instaince.WriteMainLog("机器人断开");
                _BreakConnState = true;
            }
        }
    }
}
